package cn.gogoyc.org.service.impl;

import cn.gogoyc.org.basic.servie.impl.BaseServiceImpl;
import cn.gogoyc.org.basic.util.Md5Utils;
import cn.gogoyc.org.basic.util.StrUtils;
import cn.gogoyc.org.domain.Employee;
import cn.gogoyc.org.mapper.EmployeeMapper;
import cn.gogoyc.org.service.EmployeeService;
import cn.gogoyc.user.domain.Logininfo;
import cn.gogoyc.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * The type Department service.
 * 员工Service层
 *
 * @author Yang
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        //随机生成盐值，加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5Password = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);

        //把employee对象的中数据设置到logininfo表中
        Logininfo logininfo = new Logininfo();
        //直接拷贝进去
        BeanUtils.copyProperties(employee,logininfo);
        //拷贝完之后还有些属性没赋值的手动赋值
        logininfo.setType(0);//employee表是员工表所以设置成0为管理员
        logininfo.setDisable(true);//添加之后开启
        //1.先添加loginfo对象自增长之后 才能获取employee的logininfo_id
        logininfoMapper.save(logininfo);
        //2.设置employee的logininfo_id在添加
        employee.setLogininfo_id(logininfo.getId());

        super.save(employee);
        //employee_id=自增长的id  role_id=前端传递过来了，所以直接添加就行了
        //下拉框选择的角色对应的id
        employeeMapper.addRole(employee);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.remove(employee.getLogininfo_id());
        //删除中间表对应数据,不产生数据库垃圾
        employeeMapper.removeRoleByEmployeeId(id);
        super.remove(id);
    }

    @Override
    public void update(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //自己查自己 --- 拿到原来的信息
        Employee oldEmployee = employeeMapper.loadById(employee.getId());

        //修改操作没有修改日志id和店铺id 将老对象的日志id和店铺id给新对象=没变化
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);

        //根据当前员工查出登录信息表对象
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        //将当前员工的属性信息复制给登录信息对象--覆盖
        BeanUtils.copyProperties(employee,oldLogininfo);
        //再设置id值,是当前员工对象的登录信息Id -- 关联上
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        //用修改前对象的状态
        oldLogininfo.setDisable(oldLogininfo.getDisable());
        //添加进数据库
        logininfoMapper.update(oldLogininfo);

        //先删除中间表
        employeeMapper.removeRoleByEmployeeId(employee.getId());
        //再添加
        employeeMapper.addRole(employee);
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }
}
